package com.jintoufs.web.action.baseInfo;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.domain.baseinfo.Customer;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.baseinfo.CustomerService;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2016/9/1 0001.
 */
@Controller
@Scope("prototype")
@RequestMapping("/customer")
public class CustomerAction extends SupportAction {
    @Resource
    private CustomerService customerService;


    @RequestMapping("/list")
    public String list() throws IOException {
        return "baseinfo/customer-list";
    }

    @RequestMapping("/query")
    @ResponseBody
    public JqgridPage query(int page, int rows, String sidx, String sord, String filters) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_ = null;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Customer> list = customerService.getByFilter(filters_);
        // 用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }

    @RequestMapping("/oper")
    @SystemControllerLog(description = "客户")
    public void oper(HttpServletResponse response, String oper, Customer customer, String photo) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            if (oper.equals("del")) {
                customerService.delete(customer.getId());
            } else {
                if (oper.equals("edit")) {
                    customerService.update(customer);
                    ajaxMsg.setCode("200");
                    ajaxMsg.setMessage("更新成功");
                } else if (oper.equals("add")) {
                    Customer tmp = customerService.add(customer);
                    if (tmp == null) {
                        ajaxMsg.setCode(AjaxMsg.FAILURE);
                        ajaxMsg.setMessage("添加失败");
                    } else {
                        ajaxMsg.setCode(AjaxMsg.SUCCESS);
                        ajaxMsg.setMessage("添加成功");
                    }
                } else {
                    ajaxMsg.setCode(AjaxMsg.FAILURE);
                    ajaxMsg.setMessage("操作代码错误！系统只提供增加/修改/删除功能。");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("添加失败");
        }
        writeInfo(response, JSON.toJSONString(ajaxMsg));
    }

    /**
     * input.do?id= 进入类目编辑页面 <br>
     * input.do 进入类目新增页面（不包含选中父类目）
     *
     * @param id    类目Id
     * @param model
     * @return vm/category/input.vm
     */
    @RequestMapping("input")
    public String input(Long id, Model model) {
        Customer result = null;
        String title;

        // 是否为编辑保存
        String isEdit;

        // 修改
        if (id != null && id != 0) {
            title = "修改";
            result = customerService.getById(id);

            isEdit = "edit";
        } else {
            title = "新增";
            isEdit = "add";
        }

        model.addAttribute("isEdit", isEdit);
        model.addAttribute("result", result);
        model.addAttribute("title", title);

        return "baseinfo/customer-input";
    }

    @RequestMapping("/delete")
    @SystemControllerLog(description = "删除客户")
    public void delete(HttpServletResponse response, Long[] ids) {
        for (long id : ids) {
            customerService.delete(id);
        }
        writeInfo(response, JSON.toJSONString(new AjaxMsg()));
    }

    /**
     * 初始化用户密码
     */
    @RequestMapping("/initPassword")
    @ResponseBody
    @SystemControllerLog(description = "初始化用户密码")
    public AjaxMsg initPassword(Long id) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        if (id != null && id != 0) {
            Customer customer = customerService.initPassword(id);
        }
        ajaxMsg.setCode(AjaxMsg.SUCCESS);
        return ajaxMsg;
    }


    /**
     * 获取用户详细信息
     */
    @RequestMapping("/getDetail")
    @SystemControllerLog(description = "查看用户详细信息")
    public String getDetail(Long id, Model model) {
       Customer result = new Customer();
        if (id != null && id != 0) {
            result = customerService.getById(id);
        }
        model.addAttribute("result", result);
        model.addAttribute("isEdit", "view");
        model.addAttribute("title", "客户详细信息");
        return "baseinfo/customer-input";
    }
}
